// CANSampleDlg.h : header file
//

#if !defined(AFX_CANSampleDLG_H__A3DE77F7_BCC6_4F7D_A26A_29C45E6FD742__INCLUDED_)
#define AFX_CANSampleDLG_H__A3DE77F7_BCC6_4F7D_A26A_29C45E6FD742__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxmt.h>
#include "api\candll.h"

#define BYTES_USED_PER_FRAME	24	// bytes used per frame in CAN driver buffer.

/////////////////////////////////////////////////////////////////////////////
/*
typedef HANDLE (_stdcall *pCANOpen)(int ChannelsIndex,HANDLE *hReceiveEvent);
typedef int (_stdcall *pCANClose)(HANDLE hDevice,HANDLE hReceiveEvent);
typedef int (_stdcall *pCANInit)(HANDLE hDevice,PCAN_INIT_INFO pCANInitInfo);
typedef int (_stdcall *pCANGetRegister)(HANDLE hDevice,unsigned short CANAddress,byte *pValue);
typedef int (_stdcall *pCANSetRegister)(HANDLE hDevice,unsigned short CANAddress,byte Value);
typedef int (_stdcall *pCANStart)(HANDLE hDevice);
typedef int (_stdcall *pCANReset)(HANDLE hDevice);
typedef DWORD (_stdcall *pCANTransmit)(HANDLE hDevice,PCAN_DATA_INFO pTransCANDataInfo,unsigned long Length);
typedef DWORD (_stdcall *pCANReceive)(HANDLE hDevice,PCAN_DATA_INFO pRcvCANDataInfo,unsigned long Length,unsigned long WaitTime);
typedef int (_stdcall *pCANStop)(HANDLE hDevice);
typedef int (_stdcall *pCANGetLastErrorInfo)(HANDLE hDevice,PCAN_ERROR_INFO pCANErrorInfo);
typedef int (_stdcall *pCANGetStatusInfo)(HANDLE hDevice,PCAN_STATUS_INFO pCANStatusInfo);
typedef int (_stdcall *pCANClearBuffer)(HANDLE hDevice,int BufferType);
*/


#define MAX_CAN_CHANNEL 	6
#define MAX_CAN_DEVICES		6
typedef ULONG (__stdcall *pCanDeviceOpen)(ULONG device );
typedef void  (__stdcall *pCanDeviceClose)(ULONG device );
typedef ULONG (__stdcall *pCanCANInit)(ULONG device,ULONG ChannelsIndex,PCAN_INIT_INFO pCANInitInfo);
typedef ULONG (__stdcall *pCanCANGetRegister)(ULONG device,ULONG ChannelsIndex,unsigned short CANAddress,byte *pValue);
typedef ULONG (__stdcall *pCanCANSetRegister)(ULONG device,ULONG ChannelsIndex,unsigned short CANAddress,byte Value);
typedef ULONG (__stdcall *pCanCANReset)(ULONG device,ULONG ChannelsIndex);
typedef DWORD (__stdcall *pCanCANTransmit)(ULONG device,ULONG ChannelsIndex,PCAN_DATA_INFO pTransCANDataInfo,DWORD Length);
typedef DWORD (__stdcall *pCanCANReceive)(ULONG device,ULONG ChannelsIndex,PCAN_DATA_INFO pRcvCANDataInfo,DWORD Length,unsigned long WaitTime);
typedef ULONG (__stdcall *pCanCANGetLastErrorInfo)(ULONG device,ULONG ChannelsIndex,PCAN_ERROR_INFO pCANErrorInfo);
typedef ULONG (__stdcall *pCanCANGetStatusInfo)(ULONG device,ULONG ChannelsIndex,PCAN_STATUS_INFO pCANStatusInfo);



#include <vector>
#include <map>
using namespace std;
//one CAN device can have many channels
typedef struct can_dev_struct {
	int can_id;
	vector<int> channel;
}can_dev_struct;


//typedef can_dev

/////////////////////////////////////////////////////////////////////////////
// CCANSampleDlg dialog

class CCANSampleDlg : public CDialog
{
// Construction
public:
	void Receive();
	void Transmit();
	void OnButtonHelp();
	
	CCANSampleDlg(CWnd* pParent = NULL);	// standard constructor
	BOOL PreTranslateMessage(MSG* pMsg);
public:
	int DetectCanDevices();
	int ReOpenAndInitDevices();
	void LogIntoEditWin(CString str);
public:

	CStatusBarCtrl	*m_StatusBar;
	CAN_INIT_INFO	m_CANInitInfo;
	CAN_DATA_INFO	m_TransCANDataInfo;
	CAN_DATA_INFO	m_RcvCANDataInfo;
	CAN_STATUS_INFO	m_CANStatusInfo;
	bool m_CANOpened;
	bool m_CANStarted;
	int m_DeviceType;
	int	m_currentChannel;
	int m_curCanDevice;
	 // available can devices, this struct will init by detect function
	vector<can_dev_struct> m_available_can;
	unsigned long m_TransDataLength;
	unsigned long m_TransTime;
	unsigned long m_Number;
	unsigned long m_TransBytes;
	unsigned long m_ReceiveBytes;
	byte *m_pData;

// Dialog Data
	//{{AFX_DATA(CCANSampleDlg)
	enum { IDD = IDD_CANSample_DIALOG };
	CButton	m_Radio_DataType;
	CStatic	m_Static_MemoryUsed;
	CComboBox	m_ComBo_CANChannels;
	CComboBox	m_Combo_CANDevices;
	CButton	m_Check_Sleep;
	CEdit	m_Edit_BTR1;
	CEdit	m_Edit_BTR0;
	CButton	m_Button_Start;
	CButton	m_Button_Reset;
	CButton	m_Check_IDIncrease;
	CButton	m_Radio_TransSingle;
	CButton	m_Button_Transmit;
	CButton	m_Button_Stop;
	CComboBox	m_ComBo_Mode;
	CEdit	m_Edit_TransTimes;
	CListCtrl	m_List;
	CEdit	m_Edit_SlotTime;
	CEdit	m_Edit_FrameNumber;
	CEdit	m_Edit_FrameID;
	CEdit	m_Edit_FrameData;
	CEdit	m_Edit_AccMask;
	CEdit	m_Edit_AccCode;
	CEdit	m_Edit_Log_Win;
	CComboBox	m_ComBo_WorkMode;
	CComboBox	m_ComBo_TransType;
	CComboBox	m_ComBo_FrameType;
	CComboBox	m_ComBo_FrameFormat;
	CComboBox	m_ComBo_Filter;
	CComboBox	m_ComBo_BaudRate;
	CString	m_strComBo_BaudRate;
	CString	m_strComBo_Filter;
	CString	m_strComBo_FrameFormat;
	CString	m_strComBo_FrameType;
	CString	m_strComBo_TransType;
	CString	m_strComBo_WorkMode;
	CString	m_strEdit_AccCode;
	CString	m_strEdit_AccMask;
	CString	m_strEdit_Log_Windows;
	CString	m_strComBo_Mode;
	CString	m_strEdit_FrameData;
	CString	m_strEdit_FrameID;
	UINT	m_nEdit_FrameNumber;
	UINT	m_nEdit_SlotTime;
	UINT	m_nEdit_TransTimes;
	BOOL	m_bCheck_IDIncrease;
	BOOL	m_bCheck_flag_all_recv;
	BOOL	m_bCheck_flag_all_send;
	CString	m_strEdit_BTR0;
	CString	m_strEdit_BTR1;
	UINT	m_nEdit_RxBufSize;
	UINT	m_nEdit_TxBufSize;
	int		m_nRadio_DataType;
	int		m_nRadio;
	BYTE	m_byteEdit;
	CString	m_strCombo_CANChannels;
	CString	m_strCombo_CANDevices;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CCANSampleDlg)
	public:
	virtual BOOL DestroyWindow();
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;
	HINSTANCE m_hDLL;
	bool m_bLoadDll;

	HANDLE m_hTransmitThread;
	HANDLE m_hReceiveThread;
	//HANDLE m_hDevice;
	HANDLE m_hReceiveEvent;

	CCriticalSection m_Cs;
	bool m_TransmitStop;
	bool m_RecvThreadExitFlag;

	pCanDeviceOpen m_FuncCanDevOpen;
	pCanDeviceClose m_FuncCanDevClose;
	pCanCANInit	m_FuncCanDevInit;
	pCanCANGetRegister m_FuncCanDevGetRegister;
	pCanCANSetRegister	m_FuncCanDevSetRegister;
	pCanCANReset	m_FuncCanDevReset;
	pCanCANTransmit m_FuncCanDevTransmit;
	pCanCANReceive m_FuncCanDevReceive;
	pCanCANGetLastErrorInfo m_FuncCanDevGetLastErrorInfo;
	pCanCANGetStatusInfo m_FuncCanDevGetStatusInfo;


	// Generated message map functions
	//{{AFX_MSG(CCANSampleDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnSelchangeComboWorkMode();
	afx_msg void OnButtonStart();
	afx_msg void OnButtonReset();
	afx_msg void OnButtonTransmit();
	afx_msg void OnButtonStop();
	afx_msg void OnButtonClear();
	afx_msg void OnSelchangeComboFrameFormat();
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnSelchangeComboMode();
	afx_msg void OnRadioTransSingle();
	afx_msg void OnRadioTransMulti();
	afx_msg void OnKillfocusEditFrameNumber();
	afx_msg void OnKillfocusEditFrameId();
	afx_msg void OnSelchangeComboFrameType();
	afx_msg void OnSelchangeComboBaudRate();
	afx_msg void OnSelchangeComboFilter();
	afx_msg void OnChangeEditAccCode();
	afx_msg void OnChangeEditAccMask();
	afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo);
	afx_msg void OnSelchangeComboCanChannels();
	afx_msg void OnChangeEditTxbufsize();
	afx_msg void OnChangeEditRxbufsize();
	afx_msg void OnRadioDataType();
	afx_msg void OnRadioAscii();
	afx_msg void OnBtnDetectDev();
	afx_msg void OnEditchangeComboCanDevices();
	afx_msg void OnChkFlagAllSend();
	afx_msg void OnChkFlagAllRecv();
	afx_msg void OnSelchangeComboCanDevices();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CANSampleDLG_H__A3DE77F7_BCC6_4F7D_A26A_29C45E6FD742__INCLUDED_)
